<?php
namespace app\backend\controller;

class RecycleCargo extends Backend {
	
	/**
	 * 首页
	 */
	public function index() {
		
		$view = \think\View::instance();
		$request = \think\Request::instance();
		
		$where = array();
		$condition = array();
		$condition['title'] = $request->get('title', '');
		if(0 < mb_strlen($condition['title'], 'utf8')){
			$where['recycle_cargo.title'] = array('like', '%%'.$condition['title'].'%%');
		};
		$view->assign('condition', $condition);
		
		$field = array(
			'recycle_cargo.*',
		);
		$cargo_select = \app\common\model\RecycleCargoModel::field($field)
			->where('recycle_cargo.is_del', 0)
			->where($where)
			->order('recycle_cargo.id DESC')
			->paginate(10, false, array('query'=>$condition));
		$view->assign('page', $cargo_select->render());
		
		$cargo_list = array();
		foreach($cargo_select as $RecycleCargoModel){
			$cargo = $RecycleCargoModel->toArray();
			$cargo_list[] = $cargo;
		};
		$view->assign('data_list', $cargo_list);
		
		unset($where, $condition, $field, $cargo_select, $RecycleCargoModel);
		return $view->fetch();
		
	}
	
	/**
	 * 创建
	 */
	public function create() {
		
		$request = \think\Request::instance();
		if($request->isAjax()){
			
			$parameter = \think\Request::instance()->post();
			$judge_1 = array_key_exists('name', $parameter) && is_string($parameter['name']) && (0 < strlen($parameter['name']));
			$judge_2 = array_key_exists('image', $parameter) && is_string($parameter['image']) && (0 < strlen($parameter['image']));
			$judge_3 = array_key_exists('sort', $parameter) && is_integer($parameter['sort']) && (0 <= $parameter['sort']);
			if(!$judge_1 || !$judge_2 || !$judge_3){
				return $this->disposeJson(1, '参数校验失败');
			};
			
			$parameter['image'] = explode('-', $parameter['image']);
			if(1 < count($parameter['image'])){
				return $this->disposeJson(1, '只能上传一张图片');
			};
			
			// 保存图片
			$target_path = ROOT_PATH . 'public' . DS . 'resource' . DS . 'image' . DS . 'recycle_cargo' . DS;
			$url_path = '/' . 'resource' . '/' . 'image' . '/' . 'recycle_cargo' . '/';
			$result = \Image::base64SaveFile($parameter['image'][0], $target_path, $return_message);
			if(true !== $result){
				return $this->disposeJson(1, '保存图片失败，' . $return_message);
			};
			$parameter['image'][0] = $url_path . $return_message;
			
			// 保存数据
			$time = time();
			$RecycleCargoModel = new \app\common\model\RecycleCargoModel();
			$RecycleCargoModel->name = $parameter['name'];
			$RecycleCargoModel->image = $parameter['image'][0];
			$RecycleCargoModel->sort = $parameter['sort'];
			$RecycleCargoModel->create_time = $time;
			$RecycleCargoModel->is_disable = 0;
			$RecycleCargoModel->is_del = 0;
			$result = $RecycleCargoModel->save();
			if(false == $result){
				return $this->disposeJson(1, '保存数据失败');
			};
			
			return $this->disposeJson(0, '操作成功，即将返回');
			
		}else{
			
			$view = \think\View::instance();
			
			$view->assign('recycle_cargo', \think\Config::get('upload_image.recycle_cargo'));
			
			return $view->fetch();
		};
		
	}
	
	/**
	 * 删除
	 */
	function del() {
		
		$request = \think\Request::instance();
		if($request->isAjax()){
			$parameter = \think\Request::instance()->post();
			$judge_1 = array_key_exists('id', $parameter) && is_integer($parameter['id']) && (0 < $parameter['id']);
			if(!$judge_1){
				return $this->disposeJson(1, '参数校验失败');
			};
			
			$RecycleCargoModel = \app\common\model\RecycleCargoModel::get($parameter['id']);
			if(null === $RecycleCargoModel){
				return $this->disposeJson(1, '该目标不存在，可能已经被删除');
			};
			
			$RecycleCargoModel->is_del = 1;
			$result = $RecycleCargoModel->save();
			if(false == $result){
				return $this->disposeJson(1, '保存数据失败');
			};
			
			return $this->disposeJson(0, '操作成功，即将刷新');
		};
	}
	
}